﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wise.Entities;
using Wise.Util;

namespace Wise.Entities
{
    public class MyDbConfig : JsonConfig<MyDbConfig>
    {
        internal readonly Dictionary<String, dynamic> MyCache = new Dictionary<string, dynamic>();

        public MyDbConfig()
        {
        }
        public MyDbConfig(string fileFullName) : base(fileFullName)
        {

        }

        public string 日志目录 { get; set; }

        public string 源文件暂存夹 { get; set; }

        public string 目标文件暂存夹 { get; set; }


        public bool 是测试 { get; set; }

        const string DEFAULT_新增表默认前缀 = "Z__";
        string _新增表默认前缀 = DEFAULT_新增表默认前缀;
        public string 新增表前缀
        {
            get { return _新增表默认前缀; }
            set
            {
                _新增表默认前缀 = value;
                if (string.IsNullOrEmpty(_新增表默认前缀))
                    _新增表默认前缀 = DEFAULT_新增表默认前缀;
            }
        }

        string _新增字段默认前缀 = DEFAULT_新增表默认前缀;

        public int 最大迁移行数 { get; set; } = Int32.MaxValue;

        public string 新增字段前缀
        {
            get { return _新增字段默认前缀; }
            set
            {
                _新增字段默认前缀 = value;
                if (string.IsNullOrEmpty(_新增字段默认前缀))
                    _新增字段默认前缀 = DEFAULT_新增表默认前缀;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool 一次性读取所有数据 { get; set; }

        public 数据库配置 数据源 { get; private set; } = new 数据库配置();

        public 数据库配置 目标数据库 { get; private set; } = new 数据库配置();

        public List<表映射> 表映射 { get; private set; } = new List<表映射>();

        public string 文件服务器 { get; set; }

        /// <summary>
        /// 从Excel表加载映射关系
        /// </summary>
        /// <param name="fileName"></param>
        public void LoadMap(string fileName)
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                DataSet ds = ExcelHelper.ExcelImport(fs);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Columns.Count == 2)
                    {
                        表映射 b = new 表映射();
                        b.源表名 = dt.Columns[0].ColumnName;
                        b.目标表名 = dt.Columns[1].ColumnName;
                        b.字段映射.AddRange(dt.Rows.Cast<DataRow>().Select(x => new 字段映射()
                        {
                            新数据库字段 = x[1].ToString()
                            ,
                            旧数据库字段 = x[0].ToString()
                        }));
                    }
                }
            }

        }

        public void InitMapping()
        {

        }

        public IDbConnection SourceConnection { get; set; }

        public IDbConnection TargetConnection { get; set; }

        public Func<IDbConnection> CreateTargetConnection = null;
        public Func<IDbConnection> CreateSourceConnection = null;

        public void CloseConnections()
        {
            if (SourceConnection != null && SourceConnection.State != ConnectionState.Closed)
            {
                SourceConnection.Close();
                SourceConnection.Dispose();
            }
            if (TargetConnection != null && TargetConnection.State != ConnectionState.Closed)
            {
                TargetConnection.Close();
                TargetConnection.Dispose();
            }

        }

        public void Reset()
        {
            foreach (var item in 表映射)
            {
                item.Reset();
            }
        }


        public void SetMessageForOperator(表映射 _table,string message)
        {
            int index = MyDbConfig.Current.表映射.IndexOf(_table);
            int total = MyDbConfig.Current.表映射.Count;
            string name = string.Format("{0}=>{1}", _table.源表名, _table.目标表名);
            _operator.Message = string.Format("{0}/{1}.{2}.{3}", index, total, name, message);
        }

        private dynamic _operator = null;

        internal void _SetOperator(dynamic value)
        {
            _operator = value;
        }

        //public Func<IDbConnection> CreateTargetConnection { get; set; }

        //public Func<IDbConnection> CreateSourceConnection { get; set; }
    }




}
